package forms;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class ApplicantForm extends ActionForm implements Serializable{
	private String firstname = null;
	private String middlename= null;
	private String lastname	= null;
	private String country1;	// country of citizenship
	private String country2; 	// country of residence
	
	private String address;
	private String city;
	private String state;		// country telephone code instead, if country of residence not US
	private String countrycode ;
	private String zipcode;
	private String areacode;
	private String telephonenumber;
	
	private String residencystatus;
	private String specialization;
	
//	public void reset(ActionMapping mapping, HttpServletRequest request) {
//		//setFirstname(null);
//		//setMiddlename(null);
//		//setLastname(null);
//		//setCountry1(null);
//		//setCountry2(null);
//		//setAddress(null);
//		//setCity(null);
//		//setState(null);
//		setCountrycode(null);
//		//setZipcode(null);
//		//setAreacode(null);
//		//setTelephonenumber(null);
//		//setResidencystatus(null);
//		//setDegreelocation(null);
//	}

	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	
	public String getMiddlename() {
		return middlename;
	}
	public void setMiddlename(String middlename) {
		this.middlename = middlename;
	}
	
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	
	
	
	
	/**
	 * Validate the input data. If validation fails, then report the errors in
	 * students.jsp, and specifically where <html:errors/> is located.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
	
		
		if (mapping.getInput().equals("/login/students/pages/namePage.jsp") ){
		
			if ((firstname == null) || (firstname.length() < 1))
				errors.add("firstMsgTag1", new ActionMessage("errors.required",
						"First Name"));
			else  {
				boolean containsNumber = false;
				for (int i = 0; i < firstname.length(); i++)  {
					if (Character.isDigit(firstname.charAt(i)))
							containsNumber = true;
				}
				if (containsNumber == true)
					errors.add("fnMsgTag1", new ActionMessage("errors.containsNumber", "First Name"));
			}
			
			if ((lastname == null) || (lastname.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Last Name"));
			else  {
				boolean containsNumber = false;
				for (int i = 0; i < lastname.length(); i++)  {
					if (Character.isDigit(lastname.charAt(i)))
						containsNumber = true;
				}
				if (containsNumber == true)
					errors.add("lnMsgTag1", new ActionMessage("errors.containsNumber", "Last Name"));
			}
				
			boolean containsNumber = false;
			if ((middlename != null) || (lastname.length() > 1))  {
				for (int i = 0; i < middlename.length(); i++)  {
					if (Character.isDigit(middlename.charAt(i)))
						containsNumber = true;
				}
				if (containsNumber == true)
					errors.add("mnMsgTag1", new ActionMessage("errors.containsNumber", "Middle Name"));
			}
		}
		
		if (mapping.getInput().equals("/login/students/pages/addressPage.jsp")){

			if ((address == null) || (address.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Address"));
			
			if ((city == null) || (city.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"City"));
			
			if ((countrycode == null) || (countrycode.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Country Code"));
			else
				try{
					Integer.parseInt(countrycode);
				}catch (NumberFormatException ex){
					errors.add("ccMsgTag1", new ActionMessage("errors.notanumber", "Country Code"));
				}
			
			if ((zipcode == null) || (zipcode.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Zip Code"));
			else
				try{
					Integer.parseInt(zipcode);
				}catch (NumberFormatException ex){
					errors.add("zipMsgTag1", new ActionMessage("errors.notanumber", "Zip Code"));
				}
			
			if ((areacode == null) || (areacode.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Area Code"));
			else
				try{
					Integer.parseInt(areacode);
				}catch (NumberFormatException ex){
					errors.add("areaMsgTag1", new ActionMessage("errors.notanumber", "Area Code"));
				}
			
			if ((telephonenumber == null) || (telephonenumber.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Telephone Number"));
			else
				try{
					telephonenumber=telephonenumber.replace(" ","").replace("-", "");
					Integer.parseInt(telephonenumber);
				}catch (NumberFormatException ex){
					errors.add("phoneMsgTag1", new ActionMessage("errors.notanumber", "Phone Number"));
				}
		}
		
		if (mapping.getInput().equals("/login/students/pages/USaddressPage.jsp")){

			if ((address == null) || (address.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Address"));
			
			if ((city == null) || (city.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"City"));
			
			if (((state == null) || (state.length() < 1)) && country1.equals("United States"))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"State"));
			
			if ((zipcode == null) || (zipcode.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Zip Code"));
			else
				try{
					Integer.parseInt(zipcode);
				}catch (NumberFormatException ex){
					errors.add("zipMsgTag1", new ActionMessage("errors.notanumber", "Zip Code"));
				}
			
			if ((areacode == null) || (areacode.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Area Code"));
			else
				try{
					Integer.parseInt(areacode);
				}catch (NumberFormatException ex){
					errors.add("areaMsgTag1", new ActionMessage("errors.notanumber", "Area Code"));
				}
			
			if ((telephonenumber == null) || (telephonenumber.length() < 1))
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Telephone Number"));
			else
				try{
					Integer.parseInt(telephonenumber);
				}catch (NumberFormatException ex){
					errors.add("phoneMsgTag1", new ActionMessage("errors.notanumber", "Phone Number"));
				}
		}
		
		return errors;
	}
	
	
	

	public String getCountry1() {
		return country1;
	}
	public void setCountry1(String country1) {
		this.country1 = country1.replace("_", " ");
	}
	
	public String getCountry2() {
		return country2;
	}
	public void setCountry2(String country2) {
		this.country2 = country2.replace("_", " ");
	}
	
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	
	public String getCountrycode() {
		return countrycode;
	}
	public void setCountrycode(String countrycode) {
		this.countrycode = countrycode;
	}
	
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	
	public String getAreacode() {
		return areacode;
	}
	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}
	
	public String getTelephonenumber() {
		return telephonenumber;
	}
	public void setTelephonenumber(String telephonenumber) {
		this.telephonenumber = telephonenumber;
	}
	
	
	
	public String getResidencystatus() {
		return residencystatus;
	}
	public void setResidencystatus(String residencystatus) {
		this.residencystatus = residencystatus;
	}
	
	public String getSpecialization() {
		return specialization;
	}
	public void setSpecialization(String specialization) {
		this.specialization = specialization;
	}
	
	
	
}